
package com.amigo.online.provider.merchant.mongo.dao.impl;

import java.util.List;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.provider.merchant.mongo.dao.MerchantDao;
import com.mongodb.BasicDBObject;
import com.mongodb.client.AggregateIterable;

/**
 * <code>MerchantDaoImpl.java</code>
 * <p>
 *
 * </p>
 * <p>
 *
 * @author LuChen
 * @version 1.0
 * @company Amigo
 * @time 2019年1月30日 下午6:22:37
 * @lastUpdate 无
 * </p>
 */
@Service
public class MerchantDaoImpl implements MerchantDao {

    @Autowired
    @Qualifier("readMerchantMongoTemplate")
    MongoTemplate readMongoTemplate;


    @Override
    public AggregateIterable<Document> findAroundMerchant(String collection, Double[] point, int searchMaxdistance,
                                                          BasicDBObject query, List<BasicDBObject> pipeline, int totalCount) {
        BasicDBObject aggregate = new BasicDBObject("$geoNear",
                new BasicDBObject("near",
                        new BasicDBObject("type", "Point").append("coordinates", point))
                        .append("distanceField", "distance")
                        .append("query", query)
                        .append("num", totalCount)
                        .append("spherical", true));
        pipeline.add(0, aggregate);
        AggregateIterable<Document> iterators = readMongoTemplate.getCollection(collection).aggregate(pipeline);
        return iterators;
    }

}

